package com.meicet.adapter.databinding.adapter

import android.graphics.Color
import android.graphics.Rect
import android.view.View
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.fondesa.recyclerviewdivider.dividerBuilder
import com.meicet.adapter.utils._i
import com.meicet.adapter.utils.dip
@BindingAdapter("adapter")
fun setAdapter(recyclerView: RecyclerView, adapter: BaseQuickAdapter<*, *>?) {
    recyclerView.adapter = adapter
}

@BindingAdapter("adapterBQ")
fun setAdapterBQ(recyclerView: RecyclerView, adapter: BaseQuickAdapter<*, *>?) {
    recyclerView.adapter = adapter
}

@BindingAdapter("rvLineBottom")
fun setLineState(recyclerView: RecyclerView, sizeHeight: Float) {
//    recyclerView.addItemDecoration(LineBottom(recyclerView.context, sizeHeight))
   recyclerView.context.dividerBuilder()
       .color(Color.TRANSPARENT)
       .showLastDivider()
       .size(dip(sizeHeight))
       .build().addTo(recyclerView)
}

@BindingAdapter("rvLineColor")
fun setLineState(recyclerView: RecyclerView, color: Int) {
//    recyclerView.addItemDecoration(LineBottomColor(recyclerView.context, color))
    recyclerView.context.dividerBuilder()
        .color(color)
        .showLastDivider()
        .size(1)
        .build().addTo(recyclerView)
}

@BindingAdapter("rvGrid")
fun bindGridLayoutManager(recyclerView: RecyclerView, count: Int) {
    recyclerView.layoutManager = GridLayoutManager(recyclerView.context, count, GridLayoutManager.HORIZONTAL, false)
}

@BindingAdapter("rvLinear")
fun bindLinearLayoutManager(recyclerView: RecyclerView, count: Int) {
    recyclerView.layoutManager = LinearLayoutManager(recyclerView.context, LinearLayoutManager.HORIZONTAL, false)
    _i("$count")
}
@BindingAdapter("rvEnable")
fun bindLinearLayoutManagerEnable(recyclerView: RecyclerView, count: Int) {
    _i("$count")
    recyclerView.layoutManager = object : LinearLayoutManager(recyclerView.context, VERTICAL, false) {

        override fun canScrollVertically(): Boolean {
            return false
        }

        override fun canScrollHorizontally(): Boolean {
            return false
        }
    }
    recyclerView.suppressLayout(true)
}

@BindingAdapter("rvStaggeredGrid")
fun bindStaggeredGridLayoutManager(recyclerView: RecyclerView, count: Int) {
    recyclerView.layoutManager = StaggeredGridLayoutManager(count, LinearLayoutManager.HORIZONTAL)
}

@BindingAdapter("rvItemTop0")
fun bindLayoutTop(recyclerView: RecyclerView, topDip: Int) {
    recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
//            super.getItemOffsets(outRect, view, parent, state)
            if (parent.getChildAdapterPosition(view) == 0) {
                outRect.set(0, dip(topDip), 0, 0)
            }
        }
    })
}

@BindingAdapter("rvItemTop1")
fun bindLayoutTop1(recyclerView: RecyclerView, topDip: Int) {
    recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
//            super.getItemOffsets(outRect, view, parent, state)
            if (parent.getChildAdapterPosition(view) == 1) {
                outRect.set(0, dip(topDip), 0, 0)
            }
        }
    })
}

@BindingAdapter("rvItemOffset0")
fun bindLayoutOffset0(recyclerView: RecyclerView, offsetList: List<Int>) {
    recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
            super.getItemOffsets(outRect, view, parent, state)
            if (parent.getChildAdapterPosition(view) == 0) {
                // left=  top=   right=  bottom=
                outRect.set(dip(offsetList[0]), dip(offsetList[1]), dip(offsetList[2]), dip(offsetList[3]))
            }
        }
    })
}

@BindingAdapter("rvItemOffset")
fun bindLayoutOffset(recyclerView: RecyclerView, offsetList: List<Int>) {
    recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
            super.getItemOffsets(outRect, view, parent, state)
            // left=  top=   right=  bottom=
            outRect.set(dip(offsetList[0]), dip(offsetList[1]), dip(offsetList[2]), dip(offsetList[3]))
        }
    })
}
